﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for ProductDAO
/// </summary>
public class ProductDAO
{
    /*
     * Option 1 : Disconnect UnType
     *       1. Create a connection to database
     *       2. Use SqlCommand to retrieve data from database by query
     *       3. Create a DataSet to contain data 
     *       4. Create a SqlDataAdapter to fill data from db to DataSet
     */
    public static DataTable GetAllProducts()
    {
        DataTable dt = null;
        try
        {
            SqlConnection cnn = DatabaseInteractionn.GetConnection();
            SqlCommand command = new SqlCommand(DatabaseInteractionn.SelectProduct,cnn);
            dt = new DataTable();
            SqlDataAdapter adt = new SqlDataAdapter();
            adt.SelectCommand = command;
            adt.Fill(dt);
        }
        catch (Exception)
        {

            return null;
        }
        return dt;
    }

    public static DataTable GetProductByID(string productID)
    {
        DataTable dt = null;
        try
        {
            SqlConnection cnn = DatabaseInteractionn.GetConnection();
            dt = new DataTable();
            SqlDataAdapter adt = new SqlDataAdapter(DatabaseInteractionn.SearchProductID(productID),cnn);
            adt.Fill(dt);
        }
        catch (Exception)
        {
            return null;
        }
        return dt;
    }

    /*
     * Option  2 : Disconnected Type
     *         1. Click-right to website(Project) -> add new item -> DataSet
     *         2. Create a  connection to db, drap table Products to Dataset just created
     *         3. Now, you can use table Products as objects which has already known with specific Dataset name: dsProducts
     *         4. dsProducts is Filled data by Products Table's specific - adataper: dsProductsTableAdapters
     *         5. And, all tables can know mean as "properties-object" of Dataset object: dsProducts.
     *              Products table is known by: dsProducts.ProductsDataTable object
     */
    public static DataTable GetAllProductsType()
    {
        dsProductsTableAdapters.ProductsTableAdapter adt = new dsProductsTableAdapters.ProductsTableAdapter();
        dsProducts.ProductsDataTable tb = new dsProducts.ProductsDataTable();
        adt.Fill(tb);
        return tb;
    }

    public static DataTable GetProductByNameType(string productName)
    {
        dsProductsTableAdapters.ProductsTableAdapter adt = new dsProductsTableAdapters.ProductsTableAdapter();
        dsProducts.ProductsDataTable tb = new dsProducts.ProductsDataTable();
        adt.FillDataBySearchProductName(tb,productName);
        return tb;
    }
}